import json

from api.base_service import BaseService


class OrderSettingService(BaseService):
    def __init__(self):
        super().__init__()  # 重写父类 init 方法
        # 添加本类，独有的属性
        self.service_name = "OrderSettingService"

    def add(self, list):
        resp = self.client.invoke(self.service_name, "add", list)
        # 成功: "null"  日期缺少时分秒:"Failed"  数据类型错误({}): "No"
        if resp == "null":
            return True
        else:
            return False

    def get_order_setting_by_month(self, date):
        # 使用实例，调用 invoke方法，传入 服务名、方法名、实参
        resp = self.client.invoke(self.service_name, "getOrderSettingByMonth", date)
        # 成功: "[]"   数据类型错误(2022): "Failed"
        if resp == "Failed":
            return None
        else:
            return json.loads(resp)

    def edit_number_by_date(self, data):
        # 将 用户传入的 字典数据，增加一组 k：v 数据。键为 class
        data["class"] = "com.itheima.pojo.OrderSetting"
        resp = self.client.invoke(self.service_name, "editNumberByDate", data)
        # 成功: "[]"  日期缺少时分秒:"Failed"
        if resp == "Failed":
            return False
        else:
            return True


if __name__ == '__main__':
    oss = OrderSettingService()

    list_data = [{"orderDate": "2022-8-24 00:00:00", "number": 3456}]
    resp = oss.add(list_data)
    print("响应信息:", resp)
    print("响应类型:", type(resp))
    print("*" * 50)

    resp = oss.get_order_setting_by_month("2022-08")
    print("响应信息:", resp)
    print("响应类型:", type(resp))
    print("*" * 50)

    date_data = {"orderDate": "2022-8-25 00:00:00", "number": 2468}
    resp = oss.edit_number_by_date(date_data)
    print("响应信息:", resp)
    print("响应类型:", type(resp))